package com.tianhui.consignortwofei.mvp.ui.activity.hetong.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.utils.CommonUtil;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.hetong.ContractInfo;
import com.tianhui.consignortwofei.mvp.model.hetong.ProductInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.hetong.adapter.ProductAdapter;


import org.yczbj.ycrefreshviewlib.inter.OnItemChildClickListener;
import org.yczbj.ycrefreshviewlib.item.RecycleViewItemLine;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2021/5/20
 * @description
 **/
public class ContractAddStepFourActivity extends BaseContractAddActivity {

    @BindView(R.id.activity_contract_add_step_four_contractNumberLinearLayout)
   protected LinearLayout contractNumberLinearLayout;
    @BindView(R.id.activity_contract_add_step_four_contractNumberInputItemView)
    InputItemView contractNumberInputItemView;
    @BindView(R.id.activity_contract_add_step_four_numberEditTextView)
    TextView numberEditTextView;
    @BindView(R.id.activity_contract_add_step_four_productRecyclerView)
    RecyclerView productRecyclerView;
    @BindView(R.id.activity_contract_add_step_four_emptyTipTextView)
    TextView emptyTipTextView;
    protected ProductAdapter productAdapter;

    private boolean isEditable;

    @Override
    public String getToolbarSubtitle() {
        return "添加产品";
    }

    @Override
    public void onSubtitleClick() {
        addProduct();
    }

    private void addProduct() {
        int size = productAdapter.getCount();
        if (size == 3) {
            showToast("最多添加三个");
            return;
        }
        productAdapter.add(createProductInfo());
        refreshView();
    }

    private void deleteProduct(int position) {
        int size = productAdapter.getCount();
        if (position < size) {
            productAdapter.remove(position);
            refreshView();
        }

    }

    protected void refreshView() {
        int size = productAdapter.getCount();
        if (size == 0) {
            emptyTipTextView.setVisibility(View.VISIBLE);
        } else {
            emptyTipTextView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_contract_add_step_four;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @Override
    public ContractInfo getSaveInfo() {
        return null;
    }

    @Override
    public void initData() {
        mContractInfo.isaudit = "2";
//        mContractInfo.isblack = "1";
    }

    @Override
    public void initView() {
        setTitle("添加产品");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        productRecyclerView.setLayoutManager(linearLayoutManager);


        RecycleViewItemLine itemLine = new RecycleViewItemLine(getContext(),
                LinearLayout.HORIZONTAL,
                (int) CommonUtil.convertDpToPixel(10),
                getResources().getColor(R.color.colorGrayBackground));
        productRecyclerView.addItemDecoration(itemLine);

        productAdapter = new ProductAdapter(getContext());

        productRecyclerView.setAdapter(productAdapter);

        contractNumberInputItemView.setContent(mContractInfo.contractno);
    }

    @Override
    public void initListener() {
        productAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {
                ProductInfo productInfo = productAdapter.getAllData().get(position);
                if (productInfo == null) {
                    return;
                }
                switch (view.getId()) {
                    case R.id.item_product_deleteButton:
                        deleteProduct(position);
                        break;
                }
            }
        });
    }

    private ProductInfo createProductInfo() {
        ProductInfo productInfo = new ProductInfo();
        productInfo.unit = "T";
        return productInfo;
    }

    private List<ProductInfo> getProductList() {

        return productAdapter.getAllData();
    }


    @OnClick(R.id.activity_contract_add_step_four_commitButton)
    public void commit() {
        List<ProductInfo> productInfoList = getProductList();
        if (productInfoList != null && productInfoList.size() > 0) {
            mContractInfo.contractcontentlist = productInfoList;
            for (ProductInfo info : productInfoList) {
                if (TextUtils.isEmpty(info.name)) {
                    showToast("产品名称不能为空");
                    return;
                }
                if (TextUtils.isEmpty(info.type)) {
                    showToast("规格型号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(info.factory)) {
                    showToast("生产厂家不能为空");
                    return;
                }
                if (TextUtils.isEmpty(info.unit)) {
                    showToast("计量单位不能为空");
                    return;
                }
                if (TextUtils.isEmpty(info.number)) {
                    showToast("数量不能为空");
                    return;
                }
                if (TextUtils.isEmpty(info.price)) {
                    showToast("单价不能为空");
                    return;
                }
            }

        } else {
            showToast("请添加产品信息");
            return;
        }

        if (TextUtils.isEmpty(mContractInfo.id)) {
            mContractInfo.id = "";
        }

        String contractNumber = contractNumberInputItemView.getContent();
        if (TextUtils.isEmpty(contractNumber)) {
            showToast("合同编号不能为空");
            return;
        }
        mContractInfo.contractno = contractNumber;

        requestContractCommit(mContractInfo);
    }

    @OnClick(R.id.activity_contract_add_step_four_numberEditTextView)
    public void edit() {
        isEditable = !isEditable;
        contractNumberInputItemView.setItemEnable(isEditable);
        if (isEditable) {
            numberEditTextView.setText("保存");
        } else {
            numberEditTextView.setText("编辑");
        }
    }
}
